<template>
  <el-dialog title="分配权限" :visible="isShow" @close="closeEvent">
    <!-- show-checkbox:是否显示多选框
         check-strictly:是否父子选择关联，默认是false
         default-checken-keys:[默认选择的项对应的值(这个值来自于node-key对应的字段)]
         node-key:指定选中该项后的值
     -->
    <el-tree
      v-if="bol"
      ref="tree"
      :data="treelist"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      :default-checked-keys="defaultKeys"
      node-key="id"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button>取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleById, sysRoleAssigPrem } from '@/api/setting'
export default {
  name: '',
  data() {
    return {
      isShow: false,
      treelist: [],
      defaultKeys: [],
      bol: true,
      id: ''
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    closeEvent() {
      this.isShow = false
      this.defaultKeys = []
      // 还原组件初始状态
      this.defaultKeys = []
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    // 获取所有权限数据
    async getData() {
      const res = await sysPermission()
      this.treelist = changeData(res.data, '0')
    },
    // 获取角色详情 拿到角色拥有的权限数据
    async getRoleInfo(row) {
      const res = await sysRoleById(row.id)
      this.id = row.id
      this.defaultKeys = res.data.permIds
      console.log(res, 987987)
    },
    // 点击确定修改角色权限
    async submit() {
      await sysRoleAssigPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('修改成功')
      this.closeEvent()
    }
  }
}
</script>
<style lang="less" scoped></style>
